package com.fxkj.cloud.drms.infra.config;

import com.taosdata.jdbc.TSDBDriver;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.util.DriverDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * description
 *
 * @author Wu Wei
 * @version 1.0
 * @date 2023/4/3 11:12
 * @since 1.0
 */
@Configuration
@ConfigurationPropertiesScan("com.fxkj.cloud.drms.infra.config")
@Slf4j
public class TDengineJdbcTemplateConfig {
    @Value("${fx.ts.tdengine.datasource.url}")
    private String jdbcUrl;
    @Value("${fx.ts.tdengine.datasource.driver}")
    private String driverClassName;
    @Value("${fx.ts.tdengine.datasource.username}")
    private String username;
    @Value("${fx.ts.tdengine.datasource.password}")
    private String password;
    @Value("${fx.ts.tdengine.hikari.enable}")
    private Boolean enableHikari;
    @Value("${fx.ts.tdengine.hikari.minimum-idle}")
    private Integer minIdle;
    @Value("${fx.ts.tdengine.hikari.maximum-pool-size}")
    private Integer maxPoolSize;
    @Value("${fx.ts.tdengine.hikari.connection-timeout}")
    private Long connectionTimeout;
    @Value("${fx.ts.tdengine.hikari.max-lifetime}")
    private Long maxLifetime;
    @Value("${fx.ts.tdengine.hikari.idle-timeout}")
    private Long idleTimeout;

    private Properties properties = new Properties();
    private JdbcTemplate jdbcTemplate = null;
    private DriverDataSource driverDataSource = null;
    public HikariDataSource hikariDataSource = null;

    private void initTDengineJdbcTemplate() {
        properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
        if (enableHikari) {
            HikariConfig config = new HikariConfig();
            // jdbc properties
            config.setDriverClassName(driverClassName);
            config.setJdbcUrl(jdbcUrl);
            config.setUsername(username);
            config.setPassword(password);
            // connection pool configurations
            config.setReadOnly(false);
            config.setMinimumIdle(minIdle);           //minimum number of idle connection
            config.setMaximumPoolSize(maxPoolSize);      //maximum number of connection in the pool
            config.setConnectionTimeout(connectionTimeout); //maximum wait milliseconds for get connection from pool
            config.setMaxLifetime(maxLifetime);       // maximum life time for each connection
            config.setIdleTimeout(idleTimeout);       // max idle time for recycle idle connection
            config.setConnectionTestQuery("select server_status();"); //validation query
            config.setDataSourceProperties(properties);
            config.setPoolName("TDenginePool");
            hikariDataSource = new HikariDataSource(config);
            this.jdbcTemplate = new JdbcTemplate(hikariDataSource);
        } else {
            driverDataSource = new DriverDataSource(jdbcUrl, driverClassName, properties, username, password);
            this.jdbcTemplate = new JdbcTemplate(driverDataSource);
        }
        log.info("TDengine jdbcTemplate init finish!");
    }

    public Connection getConnection() {
        try {
            if (enableHikari) {
                if (null == this.hikariDataSource) {
                    initTDengineJdbcTemplate();
                }
                return this.hikariDataSource.getConnection();
            } else {
                return this.driverDataSource.getConnection();
            }
        } catch (SQLException e) {
            log.error("TDengine get connection error!", e);
            return null;
        }
    }

    @Bean("tdJdbcTemplate")
    public JdbcTemplate TDengineJdbcTemplate() {
        if (null == jdbcTemplate) {
            initTDengineJdbcTemplate();
        }
        log.info("[fx-cloud] -| TDengineJdbcTemplate init finish!");
        return jdbcTemplate;
    }


}
